<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <div id="app">
        <page-component :pagesize="pageSize" :page="page" :total="total" @pageChange="pageChange">
        </page-component>
    </div>
</body>
<script src="https://cdn.bootcdn.net/ajax/libs/vue/2.6.12/vue.common.dev.min.js"></script>
<script>
    //注册子组件
    var pageComponent = Vue.component("page-component", {
        //传入参数
        props: {
            total: {
                type: [Number, String],
                default: 0
            },
            pagesize: {
                type: [Number, String],
                default: 1
            },
            page: {
                type: [Number, String],
                default: 1
            }
        },
        //模版
        template:
            '<div class="page">' +
            "<ul>" +
            "<li :class=\"page==1?'disable':''\" @click=\"prev\">上一页</li>" +
            '<li @click="toFirst">首页</li>' +
            '<li v-for="item in list"  :class="[item==page?\'active\':\'\']"  @click="handleClick">' +
            "{{item}}" +
            "</li>" +
            '<li @click="toEnd">尾页</li>' +
            "<li :class=\"page==totalCount?'disable':''\" @click=\"next\">下一页</li>" +
            "<li>" +
            '<input type="text" class="jumpInput" ref="jumpPage"/>' +
            "</li>" +
            '<li class="goto-label" @click.stop="gotoPage"><span>Go</span></li>' +
            '<li class="totalCount">总共{{totalCount}}页</li>' +
            "</ul>" +
            "</div>",
        //子组件data
        data: function data() {
            return {
                totalCount: 0 //总页数
            };
        },
        //根据总条目数，动态显示页面元素
        computed: {
            //list是页面上显示的1，2，3，4...5这些页码元素
            list: function list() {
                var list = []; //计算总页数
                this.totalCount = Math.ceil(this.total / this.pagesize); //超限控制
                if (this.page <= 0) {
                    this.page = 1;
                }
                if (this.page > this.totalCount) {
                    this.page = this.totalCount;
                } //计算显示页码

                if (this.totalCount > 5) {
                    if (this.page > 2 && this.page < this.totalCount - 2) {
                        list = [
                            this.page - 2,
                            this.page - 1,
                            this.page,
                            this.page + 1,
                            this.page + 2
                        ];
                    } else if (this.page <= 2 && this.page > 0) {
                        for (var i = 1; i <= 5; i++) {
                            list.push(i);
                        }
                    } else if (
                        this.page >= this.totalCount - 2 &&
                        this.page <= this.totalCount
                    ) {
                        for (var _i = 4; _i >= 0; _i--) {
                            list.push(this.totalCount - _i);
                        }
                    }
                } else {
                    for (var _i2 = 1; _i2 <= this.totalCount; _i2++) {
                        list.push(_i2);
                    }
                }
                return list;
            }
        },
        methods: {
            //页码点击事件
            handleClick: function handleClick(e) {
                var page = parseInt(e.target.innerText);
                var count = this.count,
                    total = this.total;
                this.$emit("pagechange", {
                    page: page
                });
            },
            //前一页
            prev: function prev(e) {
                if (!e.target.classList.contains("disable")) {
                    var page = this.page;
                    page > 1 && page--;
                    this.$emit("pagechange", {
                        page: page
                    });
                }
            },
            //下一页
            next: function next(e) {
                if (!e.target.classList.contains("disable")) {
                    var page = this.page;
                    page < this.totalCount && page++;
                    var pagesize = this.pagesize;
                    var total = this.total;
                    this.$emit("pagechange", {
                        page: page
                    });
                }
            },
            //首页
            toFirst: function toFirst() {
                var pagesize = this.pagesize;
                var total = this.total;
                this.$emit("pagechange", {
                    page: 1
                });
                this.$refs.jumpPage.value = "";
            },
            //尾页
            toEnd: function toEnd() {
                var maxPage = parseInt(this.total / this.pagesize);
                this.$emit("pagechange", {
                    page: maxPage,
                    pagesize: this.pagesize,
                    total: this.total
                });
                this.$refs.jumpPage.value = "";
            },
            //跳转
            gotoPage: function gotoPage(e) {
                var inputPage = parseInt(this.$refs.jumpPage.value);
                if (isNaN(inputPage) || inputPage > this.total || inputPage <= 1) {
                    this.$refs.jumpPage.value = "1";
                    this.$emit("pagechange", {
                        page: 1,
                        pagesize: this.pagesize,
                        total: this.total
                    });
                } else if (inputPage > (this.total / this.pagesize)) {
                    var maxPage = parseInt(this.total / this.pagesize);
                    this.$refs.jumpPage.value = maxPage;
                    this.$emit("pagechange", {
                        page: maxPage
                    });
                } else {
                    this.$emit("pagechange", {
                        page: inputPage
                    });
                }
            }
        }
    });
</script>
<script>
    //父组建

    new Vue({
        //el: '#app',
        data: {
            page: 1,
            pageSize: 5,
            total: 100
        },
        methods: {
            pageChange(e) {
                this.page = e.page;
                console.log(e.page);
            }
        },
        components: {
            pageComponent:pageComponent
        }
    }).$mount('#app')
</script>




<style>
    div.page {
        font-size: 12px;
        font-family: 'Franklin Gothic Medium', 'Arial Narrow', Arial, sans-serif;
        color: #666;
        user-select: none;
    }

    div.page ul {
        padding: 0;
        margin: 0;
    }

    div.page ul li {
        display: inline-block;
        min-width: 30px;
        min-height: 30px;
        border-radius: 3px;
        line-height: 30px;
        text-align: center;
        margin: 0 6px;
        cursor: pointer;
    }

    div.page ul li.active {
        background: #1796e0;
        color: #fff;
    }

    div.page ul li.disable {
        color: #ccc;
        cursor: default;
    }

    div.page .jumpInput {
        width: 30px;
        border: 1px solid;
        border-radius: 5px;
        text-align: center;
        outline: none;
    }

    div.page .totalCount {
        cursor: default;
    }

    div.page .goto-label {
        cursor: pointer;
    }

    div.page .goto-label span {
        display: inline-block;
        width: 30px;
        height: 30px;
        background-color: #1796e0;
        color: white;
        line-height: 30px;
        border-radius: 3px;
    }
</style>

</html>